package com.jcfk.eam.service.main;

import com.jcfk.eam.domain.po.base.EamRepairPlanDetailItem;
import com.jcfk.eam.domain.query.base.EamRepairPlanDetailItemQuery;
import com.jcfk.eam.domain.dto.base.EamRepairPlanDetailItemDTO;
import com.jcfk.eam.dao.base.EamRepairPlanDetailItemDao;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 计划维修备件清单
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-08-21
 */
@Service
public class EamRepairPlanDetailItemService extends BaseService<EamRepairPlanDetailItemDao,EamRepairPlanDetailItem> {

    /**
     * EamRepairPlanDetailItem分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamRepairPlanDetailItemDTO> queryByPage(DataPage<EamRepairPlanDetailItemDTO> page, EamRepairPlanDetailItemQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


        /**
     * 判断EamRepairPlanDetailItem是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamRepairPlanDetailItemDTO dto) {
        QueryWrapper<EamRepairPlanDetailItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
                                                        
        return RetResult.ok();
    }

    /**
     * 插入计划维修备件清单
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamRepairPlanDetailItemDTO dto) {

        // 保存
        EamRepairPlanDetailItem item = new EamRepairPlanDetailItem();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新计划维修备件清单
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamRepairPlanDetailItemDTO dto) {

        // 保存
        EamRepairPlanDetailItem item = new EamRepairPlanDetailItem();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除计划维修备件清单(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamRepairPlanDetailItemDTO dto) {

        EamRepairPlanDetailItem item = new EamRepairPlanDetailItem();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamRepairPlanDetailItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamRepairPlanDetailItem item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
                item.setDeleted(0);
                                                                                                                            item.setCreatedUserName(SecurityUtils.getUserName());
                                                                                }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

                                                        item.setLastUpdatedUserName(SecurityUtils.getUserName());
            
    }

    /**
     * 根据主键获取计划维修备件清单信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamRepairPlanDetailItemDTO get(String id) {
        // 查询数据
        EamRepairPlanDetailItem item = this.baseMapper.selectById(id);
            EamRepairPlanDetailItemDTO dto = new EamRepairPlanDetailItemDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }

            /**
         * 计划维修备件清单批量保存
         *
         * @param dtos
         * @return StandardCodeDTO
         */
        @Transactional(rollbackFor = Exception.class)
        public void saveBatch(List<EamRepairPlanDetailItemDTO> dtos,String  id) {
            List<EamRepairPlanDetailItem> insertList = new ArrayList<>();
            List<EamRepairPlanDetailItem> updateList = new ArrayList<>();
            List<String> notDeleteIds = new ArrayList<>();


            for (EamRepairPlanDetailItemDTO dto : dtos) {
                EamRepairPlanDetailItem item = new EamRepairPlanDetailItem();
                BeanUtils.copyProperties(dto, item);
                this.setDefaultValue(item);

                if (!StringUtils.isEmpty(dto.getId())) {
                    notDeleteIds.add(dto.getId());
                    updateList.add(item);
                } else {
                    insertList.add(item);
                }
            }

            // 删除
            QueryWrapper<EamRepairPlanDetailItem> queryWrapper = new QueryWrapper<>();
            if (notDeleteIds.size() > 0) queryWrapper.notIn("id", notDeleteIds);
            queryWrapper.lambda().eq(EamRepairPlanDetailItem::getPlanDetailId, id);
            this.remove(queryWrapper);

            // 保存新增
            if (insertList.size() > 0) this.saveBatch(insertList);

            // 保存更新
            if (updateList.size() > 0) this.updateBigBatch(updateList);
        }
    

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamRepairPlanDetailItemDTO>
     */
    public List<EamRepairPlanDetailItemDTO> getList(EamRepairPlanDetailItemQuery query) {
        // 查询条件
        QueryWrapper<EamRepairPlanDetailItem> qw = new QueryWrapper<>();
                                                            qw.lambda().eq(!StrUtil.isEmpty(query.getPlanDetailId()), EamRepairPlanDetailItem::getPlanDetailId, query.getPlanDetailId());
                                                                                                                                                                                                                                                                                                                                                                                                                                        // 查询数据
        List<EamRepairPlanDetailItem> list = super.list(qw);

        return convertList(list, EamRepairPlanDetailItemDTO.class);
    }

    /**
     * 根据主键获取计划维修备件清单信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
            EamRepairPlanDetailItem item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
        }
}

